/**
 * @param {number[][]} grid
 * @return {number}
 */
 var minPathSum = function(grid) {
  let m = grid.length,n=grid[0].length
  let dp = new Array(m+1).fill(0).map(_=>new Array(n+1).fill(Infinity))
  dp[0][1] = 0

  for(let i=1;i<=m;i++){

    for(let j=1;j<=n;j++){
      dp[i][j] = grid[i-1][j-1]+Math.min(dp[i-1][j],dp[i][j-1])
    }
  }
  
  return dp[m][n]
};

minPathSum([[1,3,1],[1,5,1],[4,2,1]])